package com.ruoyi.web.controller.mqtt;

import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author hwf
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class DefaultFuture extends CompletableFuture<Object> {
    private static final Map<String, DefaultFuture> FUTURES = new ConcurrentHashMap<>();

    private final String id;

    public DefaultFuture(String id) {
        this.id = id;
        FUTURES.put(id, this);
    }


    public static boolean contains(String msgId) {
        return FUTURES.containsKey(msgId);
    }

    public static void received(String msgId, Response msg) {
        DefaultFuture future = FUTURES.remove(msgId);
        if (future != null) {
            future.doReceived(msg);
        }

    }


    private void doReceived(Response res) {
        this.complete(res);
    }


    public static void cancel(String msgId) {
        FUTURES.remove(msgId);
    }
}
